Xbasic

json_reformat_safe Function

Syntax

c json_text = json_reformat_safe(json_text as C [, flagIndent as L])

Arguments

json_textCharacter

A string of JSON to reformat.

flagIndentLogical

Default value is .t.

Description

The json_reformat_safe() function reformats a string of JSON data with line-breaks and optional indentation.

Discussion

json_reformat_safe() wraps the lower level json_reformat() function. The json_reformat() function only accepts 'properly formed' JSON strings (property names must be double quoted and string values must be double quoted). json_reformat_safe() will automatically convert JSON strings where property names are not quoted and strings are single quoted.

Example

dim json as c 
json = <<%txt%
{firstname: 'Fred', lastname: 'Smith', Address: {
Street: '123 Main St', 
City: 'Boston', 
State: 'Ma'
}
}
%txt%

?json_reformat_safe(json)
= {
    "firstname": "Fred",
    "lastname": "Smith",
    "Address": {
        "Street": "123 Main St",
        "City": "Boston",
        "State": "Ma"
    }
}

?json_reformat_safe(json,.f.)
= {"firstname":"Fred","lastname":"Smith","Address":{"Street":"123 Main St","City":"Boston","State":"Ma"}}

See Also